/**
 * 信息处理程序
 * @author <1755885716@qq.com> Tao
 * @param {Object} param
 */
function main(param){
	if(!param) return
	let type = param.type
	delete param.type
	return new Promise(function(resolve,reject){
		switch(type){
			case 'modal':
				uni.showModal({
					success:res=>{
						if (res.confirm) {
							// 输入时返回的是输入内容
							resolve(param.editable?res.content:true)
						} else if (res.cancel) {
							resolve(false)
						}
					},
					fail:err=>resolve(false),
					...param,
				})
				break;
			case 'loading':
				uni.showLoading({
					success:res=>resolve(res),
					fail:err=>resolve(err),
					...param,
				})
				break;
			default:
				uni.showToast({
					success:res=>resolve(res),
					fail:err=>resolve(err),
					...param,
				})
		}
	})

}
/**
 * 参数聚合
 * @param {Object} param 传入参数
 * @param {Object} option 默认参数
 */
function parseParam(param,option){
	const paramIsString = typeof param=='string'
	let useParam = {}
	if(paramIsString){
		if(option.type=='model'){
			useParam.content=param
		}else{
			useParam.title=param
		}
		useParam={
			type:'message',
			...useParam,
			...option,
		}
	}else{
		useParam = {
			type:'message',
			...option,
			...param,
		}
	}
	return useParam
}
/**
 * 配置式信息 默认提示信息
 * @param {Object||String} param
 */
function Toast(param){
	let useParam = parseParam(param,{
		duration:2200,
		icon:'none'
	})
	return main(useParam)
}
/**
 * 提示信息
 * @param {Object||String} param
 */
function message(param){
	return Toast(param)
}
Toast.msg=message
Toast.message=message
/**
 * 成功提示
 * @param {Object||String} param
 */
function success(param){
	let useParam = parseParam(param,{
		duration:2200,
		icon:'success'
	})
	return main(useParam)
}
Toast.success=success
/**
 * 错误信息
 * @param {Object||String} param
 */
function error(param){
	let useParam = parseParam(param,{
		duration:2200,
		icon:'error'
	})
	return main(useParam)
}
Toast.error=error
Toast.err=error
/**
 * 加载状态
 * @param {Object||String} param
 */
function loading(param){
	let useParam = parseParam(param,{
		type:'loading'
	})
	return main(useParam)
}
Toast.loading=loading
/**
 * 询问
 * @param {Object||String} param
 */
function confirm(param){
	let useParam = parseParam(param,{
		type:'modal'
	})
	return main(useParam)
}
Toast.confirm=confirm
/**
 * 确认
 * @param {Object||String} param
 */
function alert(param){
	let useParam = parseParam(param,{
		type:'modal',
		showCancel:false,
	})
	return main(useParam)
}
Toast.alert=alert
export default Toast
